package com.lovo.homework7_12Aggregate;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapKeyValue {
    /*

    2. 给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，然后使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，
    最后统计出各字符串元素的出现次数。

    */
    public static void main(String[] args) {
        String[] str = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        int a_count=0;
        int b_count=0;
        int c_count=0;
        for (int i = 0; i < str.length; i++) {
            if (str[i].equals("a")){
                a_count++;
            }else if (str[i].equals("b")){
                b_count++;
            }else {
                c_count++;
            }
        }
        Map<String ,Integer> map =new HashMap<String ,Integer>();
        map.put("a",a_count);
        map.put("b",b_count);
        map.put("c",c_count);
        Set set = map.keySet();
        for (Object obj :  set) {
            System.out.println(obj+":"+map.get(obj)+"次");
        }
    }
}
